package com.forsir.android.mhfinance.storeObjects;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.database.Cursor;

import com.forsir.android.mhfinance.helper.DbAdapter;

public final class AccountsList {
	private static List<Account> mAccounts = new ArrayList<Account>();

	public static void setAccounts(List<Account> accounts) {
		mAccounts = accounts;
	}

	public static List<Account> getAccounts() {
		return mAccounts;
	}

	public static Account getLastAccount(int lastAccountId, DbAdapter dbHelper) {
		if (lastAccountId > 0) {
			return getAccountById(lastAccountId);
		}

		final String[] columns = new String[] { DbAdapter.PAYMENT_ACCOUNT };
		final Cursor cursor = dbHelper.fetchEntry(DbAdapter.PAYMENT_TABLE, columns, null, null, null, null, DbAdapter.ID + " desc", 1);

		if (!cursor.moveToFirst()) {
			return getFirst();
		}

		final Account account = getAccountByIdOrFirst(cursor.getLong(0));
		cursor.close();
		return account;
	}

	public static void loadAll(DbAdapter dbHelper) {
		final Cursor cursor = dbHelper.fetchAllEntries(new Account(), DbAdapter.ACCOUNT_NAME);
		mAccounts = new ArrayList<Account>();

		if (!cursor.moveToFirst()) {
			cursor.close();
			return;
		}

		do {
			getAccounts().add(new Account(cursor));
		} while (cursor.moveToNext());
		cursor.close();
	}

	public static boolean isEmpty() {
		return mAccounts.size() == 0;
	}

	public static String[] getNames() {
		final int size = getAccounts().size();
		final String[] retString = new String[size];
		for (int i = 0; i < size; i++) {
			retString[i] = mAccounts.get(i).getName();
		}
		return retString;
	}

	public static Account getAccount(int index, Account defaultValue) {
		if (index >= 0 && index < mAccounts.size()) {
			return mAccounts.get(index);
		}
		return defaultValue;
	}

	public static Account getAccount(int index) {
		return getAccount(index, null);
	}

	public static Account getAccountById(long accountId) {
		final int count = mAccounts.size();
		for (int i = 0; i < count; i++) {
			if (mAccounts.get(i).getId() == accountId) {
				return mAccounts.get(i);
			}
		}
		return null;
	}

	public static Account getAccountByIdOrFirst(long accountId) {
		final int count = mAccounts.size();
		for (int i = 0; i < count; i++) {
			if (mAccounts.get(i).getId() == accountId) {
				return mAccounts.get(i);
			}
		}
		return getFirst();
	}

	public static Account getFirst() {
		if (mAccounts.size() == 0) {
			return null;
		}

		return mAccounts.get(0);
	}

	public static List<? extends Map<String, String>> getGroup(Account lastAccount, boolean onlyIfEmpty) {
		final List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();

		final int count = mAccounts.size();
		for (int i = 0; i < count; i++) {
			final Map<String, String> curGroupMap = new HashMap<String, String>();
			// curGroupMap.put("id", String.valueOf(mAccounts.get(i).getId()));
			curGroupMap.put("name", mAccounts.get(i).getName());
			groupData.add(curGroupMap);
		}

		if (lastAccount != null && (count == 0 || (count != 0 && !onlyIfEmpty))) {
			final Map<String, String> curGroupMap = new HashMap<String, String>();
			// curGroupMap.put("id", "-1");
			curGroupMap.put("name", lastAccount.getName());
			groupData.add(curGroupMap);
		}

		return groupData;
	}

	public static void cleanSummary() {
		final int count = mAccounts.size();
		for (int i = 0; i < count; i++) {
			mAccounts.get(i).cleanSummary();
		}
	}

	public static void fillSummary(HashMap<Long, String[]> data) {
		// data must be sorted: id, count, deposit, withdraw
		final int count = mAccounts.size();
		Account account;
		String[] valuesList;
		for (int i = 0; i < count; i++) {
			if (data.containsKey(mAccounts.get(i).getId())) {
				account = mAccounts.get(i);
				valuesList = data.get(account.getId());
				account.count += Integer.parseInt(valuesList[0]);
				account.summaryDeposit += Float.parseFloat(valuesList[1]);
				account.summaryWithdraw += Float.parseFloat(valuesList[2]);
			}
		}
	}

	public static List<Map<String, String>> getGroup(String currencyBefore, String currencyAfter) {
		final List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
		final int count = mAccounts.size();
		Account account;

		for (int i = 0; i < count; i++) {
			final Map<String, String> curGroupMap = new HashMap<String, String>();
			account = mAccounts.get(i);

			curGroupMap.put("id", String.valueOf(account.getId()));
			curGroupMap.put("name", account.getName());
			if (account.count == 0) {
				curGroupMap.put("count", "");
			} else {
				curGroupMap.put("count", String.valueOf(account.count) + "x");
			}
			curGroupMap.put("deposit", Payment.formatAmountString(currencyBefore, account.summaryDeposit, currencyAfter));
			curGroupMap.put("withdraw", Payment.formatAmountString(currencyBefore, account.summaryWithdraw, currencyAfter));
			curGroupMap.put("summary", Payment.formatAmountString(currencyBefore, account.summaryDeposit + account.summaryWithdraw, currencyAfter));
			groupData.add(curGroupMap);
		}

		return groupData;
	}

	public static float getSumDeposit() {
		final int count = mAccounts.size();
		float deposit = 0;

		for (int i = 0; i < count; i++) {
			deposit += mAccounts.get(i).summaryDeposit;
		}
		return deposit;
	}

	public static float getSumWithdraw() {
		final int count = mAccounts.size();
		float withdraw = 0;

		for (int i = 0; i < count; i++) {
			withdraw += mAccounts.get(i).summaryWithdraw;
		}
		return withdraw;
	}
}